package com.example.cash.craft.controller;

import com.example.cash.craft.pojo.param.ProductAddNewParam;
import com.example.cash.craft.pojo.param.ProductUpdateParam;
import com.example.cash.craft.pojo.vo.PageData;
import com.example.cash.craft.pojo.vo.ProductVO;
import com.example.cash.craft.service.IProductService;
import com.example.cash.craft.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "4. 商品管理模块")
@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Operation(summary = "[管理] 新增商品")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@ParameterObject @Valid ProductAddNewParam param) {
        productService.addNew(param);
        return JsonResult.ok();
    }

    @Operation(summary = "[管理] 删除商品")
    @ApiOperationSupport(order = 200)
    @Parameter(name = "id", description = "商品ID", required = true, example = "1")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PostMapping("/{id}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        productService.deleteById(id);
        return JsonResult.ok();
    }

    @Operation(summary = "[管理] 修改商品")
    @ApiOperationSupport(order = 300)
    @Parameter(name = "id", description = "商品ID", required = true, example = "1")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PostMapping("/{id}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, @ParameterObject @Valid ProductUpdateParam param) {
        productService.updateById(id, param);
        return JsonResult.ok();
    }

    @Operation(summary = "[游客] 根据ID查询商品")
    @ApiOperationSupport(order = 400)
    @Parameter(name = "id", description = "商品ID", required = true, example = "1")
    @GetMapping("/{id}")
    public JsonResult<ProductVO> selectById(@PathVariable Long id) {
        ProductVO data = productService.selectById(id);
        return JsonResult.ok(data);
    }

    @Operation(summary = "[游客] 根据类别查询商品列表")
    @ApiOperationSupport(order = 410)
    @Parameter(name = "page", description = "页码", example = "1", required = false)
    @Parameter(name = "categoryId", description = "类别ID", example = "1", required = true)
    @GetMapping()
    public JsonResult<PageData<ProductVO>> listByCategoryId(Long categoryId, Integer page) {
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<ProductVO> data = productService.listByCategoryId(categoryId, page);
        return JsonResult.ok(data);
    }

    @Operation(summary = "[游客] 查询全部商品列表（临时）")
    @ApiOperationSupport(order = 419)
    @Parameter(name = "page", description = "页码", example = "1", required = false)
    @GetMapping("/list-all")
    public JsonResult<PageData<ProductVO>> listAll(Integer page) {
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<ProductVO> data = productService.listAll(page);
        return JsonResult.ok(data);
    }

    @Operation(summary = "[管理] 重建缓存")
    @ApiOperationSupport(order = 500)
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @PostMapping("/rebuild-cache")
    public JsonResult<Void> rebuildCache() {
        productService.rebuildCache();
        return JsonResult.ok();
    }

}